package aichess;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Our chess implementation
 * @author DumbStation
 */
public class AIChess {
    private static int MAX_DEPTH = 1;
    private Board board;

    /**
     * Constructor of AIChess, initialize a board. Then call readFile method.
     */
    public AIChess(){
        board = new Board();
        board.setLongCastle(Board.TURNWHITE, false);
        board.setLongCastle(Board.TURNBLACK, false);
        board.setShortCastle(Board.TURNWHITE, false);
        board.setShortCastle(Board.TURNBLACK, false);
        try {
            readFile("current.tbl");
        } catch (FileNotFoundException ex) {
            System.out.println("File not found");
        }
    }
    
    /**	Open the file and parse it
    *	@param file file to open
    **/
    public void readFile(String file) throws FileNotFoundException{ 
        int[][] b = new int[Board.BOARDSIZE][Board.BOARDSIZE];
        Scanner sc = new Scanner(new File(file));
        for(int i=0;i<Board.BOARDSIZE;i++){
            for(int j=0;j<Board.BOARDSIZE;j++){
                if(sc.hasNextInt()){
                    b[i][j] = sc.nextInt();
                }
            }
        }
        board.fromArray(b);
        
        while(sc.hasNextLine()){
            String string = sc.nextLine();
            if(string.compareTo("B") == 0) board.setTurn(Board.TURNWHITE);
            if(string.compareTo("N") == 0) board.setTurn(Board.TURNBLACK);  
            if(string.contains("MovsHastaEmpate")){
                Scanner src = new Scanner(string);
                src.next();
                board.movestodraw = src.nextInt();
                src.close();
            }
            if(string.contains("EnroqueC_N")) board.setShortCastle(Board.TURNBLACK, true);
            if(string.contains("EnroqueC_B")) board.setShortCastle(Board.TURNWHITE, true);
            if(string.contains("EnroqueL_N")) board.setLongCastle(Board.TURNBLACK, true);
            if(string.contains("EnroqueL_B")) board.setLongCastle(Board.TURNWHITE, true);
            if(string.contains("AlPaso")){
                Scanner src = new Scanner(string);
                src.next();
                board.setEnPassent(new Coord(src.next()));
                src.close();
            }
        }
        sc.close();
    }
    
    /**	Main: call the chess implementation (AIChess).
    *	@param args arguments introduced by command line
    **/
    public static void main(String[] args) {
        // TODO code application logic here
        MoveValue mv;
        AIChess chess = new AIChess();
        System.out.println("DFS -> ");
        mv = DFS.dfs(chess.board);
        System.out.println(mv);
        System.out.println();
        System.out.println("IDS -> ");
        mv = IDS.ids(chess.board, AIChess.MAX_DEPTH);
        System.out.println(mv);
    }
}
